﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Business.Common;

public partial class UserControls_File : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && !string.IsNullOrEmpty(Request["varinfo"]))
        {
            //获取改过的名称和所选路径组合成的字符串
            string varinfo = Request["varinfo"];
            //分解成数组
            string[] infoarray = varinfo.Split('|');
            //是否全部成功
            bool issuessful = false;
            //获取File文件并循环上传
            HttpFileCollection files = HttpContext.Current.Request.Files;
            for (int i = 0; i < infoarray.Length-1; i++)
            {
                Response.Write("<script>window.parent.SetStatus('" + (i + 1) + "','上传中...');</script>");
                Response.Flush();
                try
                {
                    //分解该文件的名称和路径
                    string[] PathAndName = infoarray[i].Split('^');
                    //读取文件
                    AttachmentFileUpload ObjFile = new AttachmentFileUpload();
                    if (ObjFile.readfile((HttpPostedFile)files[i]))
                    {
                        //修正路径及文件名
                        PathAndName[1] = PathAndName[1].Replace(ObjFile.GetFileExt().ToString(), "");
                        string FileExt = ObjFile.GetFileExt().ToString().TrimStart('.');
                        if (PathAndName[0].Equals("/"))
                        {
                            PathAndName[1] = "(" + FileExt + ")" + PathAndName[1];
                            PathAndName[0] = PathAndName[1];
                        }
                        else
                        {
                            PathAndName[1]="(" +FileExt+")"+ PathAndName[1];
                            PathAndName[0] = PathAndName[0] + "/"+ PathAndName[1];
                        }
                        //插入数据库
                        if (!ObjFile.Update_St_Attachment(Request["IID"], PathAndName[0], PathAndName[1], Session["UserId"].ToString(), Request["step"]))
                        {
                            Response.Write("<script>window.parent.SetStatus('" + (i + 1) + "','插入数据库出错');</script>");
                            Response.Flush();
                            issuessful = true;
                            continue;
                        }
                    }
                    else
                    {
                        Response.Write("<script>window.parent.SetStatus('" + (i + 1) + "','不允许的文件类型或文件过大');</script>");
                        Response.Flush();
                        issuessful = true;
                        continue;
                    }
                }
                catch
                {
                    Response.Write("<script>window.parent.SetStatus('" + (i + 1) + "','意外的错误');</script>");
                    Response.Flush();
                    issuessful = true;
                    continue;
                }
                Response.Write("<script>window.parent.SetStatus('" + (i + 1) + "','100%');</script>");
                Response.Flush();
            }
            if (!issuessful)
            {
                Response.Write("<script>window.parent.Finish();</script>");
                Response.Flush();
            }
            else
            {
                Response.Write("<script>window.parent.FinishCopy();</script>");
                Response.Flush();
            }
        }
        else
        {
            fileuploadC.iid = Request["iid"];
            fileuploadC.wiid = Request["wiid"];
            fileuploadC.ctlid = Request["ctlid"];
        }
    }
}
